<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class LGToolbarWidget extends CWidget {

//    private $uploadFileRoles = array(
//	LGConstantUserRole::SUPER_ADMIN,
//	LGConstantUserRole::ADMIN,
//	LGConstantUserRole::PRESIDENT,
//	LGConstantUserRole::GENERAL_SECRETARY,
//	LGConstantUserRole::VICE_PRESIDENT,
//	LGConstantUserRole::TREASURER
//    );

    public function init() {
	parent::init();
	$menuArray = array();
    }

        /**
     * 
     * @param type $roleName
     * @return array<string>
     */
    public function findAllFileAuthItemOfARole($roleName) {
	$criteria = new CDbCriteria;
	$criteria->addCondition('child LIKE "File.%"');
	$criteria->compare('parent', $roleName);

	$arrFileAuthItems = array();

	/* @var $authItemChild AuthItemChild */
	foreach (AuthItemChild::model()->findAll($criteria) as $authItemChild) {
	    $arrFileAuthItems[] = $authItemChild->child;
	}
	return $arrFileAuthItems;
    }

    
    public function run() {
	parent::run();
	
	if (isset(app()->user) && isset(app()->user->id)) {
	    $loggedUser = User::model()->findByPk(app()->user->id);

	    if (isset($loggedUser)) {


		//Show user's full name
		$welcomeItem = array(
		    'title' => '<span class="welcome">Welcome (' . $loggedUser->full_name .')</span>',
		);
		$menuArray['welcome'] = $welcomeItem;
		//View Profile link
		$viewProfileItem = array(
		    'title' => 'Your Profile',
		    'href' => url('membercenter/viewprofile')
		);
		$menuArray['profile'] = $viewProfileItem;

		//View File 
		$viewFileItem = array(
		    'title' => 'View Files',
		    'href' => url('file/index')
		);
		$menuArray['viewfile'] = $viewFileItem;

		//Check user's roles have upload file permission
		
		
		$fileAuthItems = $this->findAllFileAuthItemOfARole($loggedUser->user_role);

		
		if (in_array(LGConstantFileAuthItemName::UPLOAD_FILE, $fileAuthItems)) {
		    //View Upload file
		    $uploadItem = array(
			'title' => 'Upload File',
			'href' => url('file/uploadfile')
		    );
		    $menuArray['uploadfile'] = $uploadItem;
		}
		
		//Only show this link for users have permission
		$logoutItem = array(
		    'title' => 'Logout',
		    'href' => url('membercenter/logout')
		);
		$menuArray['logout'] = $logoutItem;

//		$currentAction = strtolower(app()->controller->action->id);
//
//		if ($currentAction == 'viewprofile') {
//		    if (isset($menuArray['profile'])) {
//			unset($menuArray['profile']['href']);
//		    }
//		} else if ($currentAction == 'uploadfile') {
//		    if (isset($menuArray['uploadfile'])) {
//			unset($menuArray['uploadfile']['href']);
//		    }
//		} else if ($currentAction == 'index') {
//		    if (isset($menuArray['viewfile'])) {
//			unset($menuArray['viewfile']['href']);
//		    }
//		}
	    }
	}

	return $this->render('_toolbar', array('menuArray' => $menuArray));
    }

}

?>
